<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Menu;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Response;
use Illuminate\Validation\ValidationException;
class MenuController extends Controller
{
    /**
     * 定义实例化变量
     * @var Menu
     */
    protected $model;
    /**
     * 实例化MENU
     * @var Menu
     */
    public function __construct()
    {
        $this->model = new Menu();

    }

    /**
     * 菜单列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(){
            return view('admin.menu.index');
    }
    public function getList(Request $request){
        $params=[
            'nocount'=>true,
            'where'=>[
                ['disabled',1]
            ],
            'order'=>'created_at','desc',
            'getfield'=>true,
            'field'=>['id','title','status','parent_id as pid','psort','menu_route','routes','description','created_at','updated_at','id as sid']
        ];
        $rs = $this->model->getList($params);
        $arr=array();
        foreach ($rs['row'] as $k=>$v){
            $arr[]=(array)$v;
        }
        if($arr){
            return response()->json(array('code'=>0,'data'=>$arr));
        }
    }

    /**
     * 显示添加菜单
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(){
        return view('admin.menu.add');
    }

    /**
     * 菜单数据提交
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addData(Request $request){
         $param = $request->input();
         $validate = [
            //规则
            'rule' => [
                'title'   => 'required|string|between:2,30',
                'status'  => 'required|integer|min:0|max:4',
                'parent_id'=>'integer',
                'description'=>'string|min:0|max:200',
            ],
            //提示信息
            'message' => [
                'title.required' => '菜单名称不能为空',
                'title.string'   => '菜单名称仅支持字符串',
                'title.between'  => '菜单名称长度介于2-30个字符',
                'status.required'=> '状态必须选择',
                'status.integer' => '状态必须是数字型',
                'status.min'    =>  '状态超出最小值',
                'status.max'   => '状态超出最大值',
                'parent_id.integer'=>'上级菜单必须是数字型',
                'description.string'=>'描述必须是字符型',
                'description.min'=>'描述小于最小值',
                'description.max'=>'描述超出字符型最大值',

            ],
            //自定义
            'custom' => [
                'title'   => '菜单名称',
                'status'  =>'状态',
                'parent_id'=>'上级菜单',
                'description'=>'菜单描述',
            ]
        ];
         //规则校验
        $validator = Validator::make( $request->all(), $validate['rule'], $validate['message'], $validate['custom'] );
        if ($validator->fails()) {
            return response()->json([ 'code'=>'300', 'msg' => $validator->errors()->first() ]);
        }
        //调用模型插入数据
        $rs = $this->model->addMenu($param);
        if($rs){
            return response()->json(array('code'=>200,'msg'=>'保存成功'));
        }else{
            return response()->json(array('code'=>300,'msg'=>'保存失败'));
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delMenu(Request $request){
        $id = $request->input('id');
        if(empty($id) || !is_numeric($id)){
            return response()->json(array('code'=>300,'msg'=>"参数为空"));
        }
        $status_rs = $this->model->getSingle(['id'=>$id,'disabled'=>1]);
        if($status_rs->status==1){
            return response()->json(array('code'=>300,'msg'=>'菜单已经启用，不能删除'));
        }
        $rs = $this->model->getSingle(['parent_id'=>$id,'disabled'=>1]);

       if($rs){
           return response()->json(array('code'=>300,'msg'=>"子菜单存在"));
       }
       $menuModel = $this->model->updateMenu($id,['disabled'=>2]);


       if($menuModel){
           return response()->json(array('code'=>200,'msg'=>'删除成功'));
       }else{
           return response()->json(array('code'=>300,'msg'=>'删除失败'));
       }
    }

}
